﻿using System.Text;
using fastdb.Common;

namespace fastdb.Db;

public class Row
{
    public Row(int colCount)
    {
        ColumnData = new List<object>(colCount);
        for (int i = 0; i < colCount; i++)
        {
            ColumnData.Add(new object());
        }
    }

    public int RowIndex { get; set; }

    /// <summary>
    /// 每行的列数据（数组格式存储）
    /// </summary>
    private List<dynamic> ColumnData { get; set; }

    public dynamic this[int index]
    {
        get => ColumnData[index];
        set => ColumnData[index] = value;
    }

    public byte[] GenerateDataBytes(List<ColumnType> columnTypes)
    {
        var res = Array.Empty<byte>();

        for (var i = 0; i < columnTypes.Count; i++)
            if (columnTypes[i] == ColumnType.INT)
                res = ByteHelper.ByteArrayJoin(res,
                    ByteHelper.ByteArrayJoin(ByteHelper.GetBytesFromNumber(Convert.ToInt32(ColumnData[i]))));
            else
                res = ByteHelper.ByteArrayJoin(res, ByteHelper.ByteArrayJoin(Encoding.UTF8.GetBytes(Convert.ToString(ColumnData[i]))));

        return res;
    }
}